const validateF = form => {
    const checkPhone = (rule, value, callback) => {
      const phoneReg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
      if (!value) {
            callback(new Error('手机号不能为空'))
        } else {
            let phoneRe = new RegExp(phoneReg)
            !phoneRe.test(value) ? callback(new Error('请输入正确的手机号')) : callback()
        }
    }

    const checkEmail = (rule, value, callback) => {
        const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
        if (!value) {
            callback(new Error('邮箱不能为空'))
        } else {
            let mailRe = new RegExp(mailReg)
            !mailRe.test(value) ? callback(new Error('请输入正确的邮箱号')) : callback()
        }
    }

    const checkPhoneOrEmail = (rule, value, callback) => {
        const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
        const phoneReg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
        if (!value) {
            callback(new Error('手机或邮箱不能为空'))
        } else {
            let mailRe = new RegExp(mailReg)
            let phoneRe = new RegExp(phoneReg)
            phoneRe.test(value) || mailRe.test(value) ? callback() : callback('请输入正确的手机或邮箱')
        }
    }

    const checkPassword = (rule, value, callback) => {
        if (!value) {
            callback(new Error('密码不能为空'))
        } else {
            let pwdReg = /^.*(?=.{6,12})(?=.*\d)(?=.*[A-Za-z]+).*$/
            !pwdReg.test(value) ? callback(new Error('密码必须包含数字和字母且长度为6~12位')) : callback()
        }
    }

    const checkPassword2 = (rule, value, callback) => {
        value === form.password ? callback(new Error('新密码与当前密码输入的密码相同')) : callback()
    }

    const validateAgree = (rule, value, callback) => {
        !value ? callback(new Error('注册需要同意用户协议、法律声明和隐私政策!')) : callback()
    }

    const validateUrl = (rule, value, callback) => {
        if (!value) {
            callback(new Error('链接不能为空'))
        } else {
            let urlReg = /^(https?:\/\/(([a-zA-Z0-9]+-?)+[a-zA-Z0-9]+\.)+[a-zA-Z]+)(:\d+)?(\/.*)?(\?.*)?(#.*)?$/
            !urlReg.test(value) ? callback(new Error('请输入正确的链接')) : callback()
        }
    }

    return {
        checkPhone,
        checkPassword,
        checkPassword2,
        validateAgree,
        checkEmail,
        checkPhoneOrEmail,
        validateUrl
    }
}
export default validateF
